<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog=true">新增角色</el-button>
            </el-row>

            <!-- 表格 -->
            <el-table :data="List">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="modifySetting(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delrow(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination :current-page="page.page" :page-size="page.pagesize" :total="page.total" layout="prev,pager,next" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增|编辑角色 -->
    <el-dialog :title="roleForm.id?'修改数据':'新增数据'" :visible.sync="showDialog" width="50%" @close="close">
      <!-- 表单结构 -->
      <el-form ref="fm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="addRole">{{ roleForm.id?'修改':'新增' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleListAPI, deleteRoleAPI, addRoleAPI, getRoleDetailAPI, updateRoleAPI } from '@/api/setting.js'
export default {
  data () {
    return {
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      List: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 2,
        total: 0 // 记录总数
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 获取数据刷新页面
    async getList () {
      const { total, rows } = await getRoleListAPI(this.page)
      this.page.total = total
      this.List = rows
    },
    changePage (newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getList()
    },
    // 删除
    async delrow (id) {
      try {
        await this.$confirm('确认删除？', '提示')
        await deleteRoleAPI(id)
        this.getList()
        this.$message.success('删除成功')
        if (this.List.length === 1 && this.page.page > 1) {
          this.page.page--
        }
      } catch (error) {
        console.log(error)
      }
    },
    async addRole () {
      try {
        this.$refs.fm.validate()
        if (this.roleForm.id) {
          await updateRoleAPI(this.roleForm)
          this.$message.success('修改成功')
        } else {
          await addRoleAPI(this.roleForm)
          this.$message.success('新增成功')
        }

        this.getList()
        this.showDialog = false
        this.page.page = 1
      } catch (error) {
        console.log(error)
      }
    },
    close () {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.fm.resetFields()
    },
    // 编辑表格
    async modifySetting (id) {
      // 数据回填
      const res = await getRoleDetailAPI(id)
      this.roleForm = res
      // console.log(res)
      this.showDialog = true
    }
  }
}
</script>
<style lang="less" scope>
</style>
